module(..., package.seeall) -- 使得文件中的函数在何处都可调用

require "pins"
require "nvm"
require "modbusrtu"
require "mqttInMsgKeson"
require "regularlyUploadData"
require "networkTiming"
require "logGenerator"
require "log"
-- 初始化nvm获取配置参数
nvm.init("config.lua", true)
SetParm = false
-- 系统参数初始化
local pKey, sn = nvm.get("PKey"), nvm.get("Sn")
-- gpio口初始化
local setGpio16Fnc = pins.setup(pio.P0_16, 1) -- 电磁阀吸合 0-正常 1-吸合
local setGpio17Fnc = pins.setup(pio.P0_17, 1) -- 报警灯 0-灯灭 1-灯亮
-- 其他参数初始化
local errInfo, sysTime, slaveaddr, instructions, reg, logInfo, setSoLen = "",
                                                                          "",
                                                                          "0x01",
                                                                          "0x06",
                                                                          "",
                                                                          "",
                                                                          false
local devtypeGet, seqGet, dc, ct, sysId, pkey, deviceTypeId, mode, longitude,
      latitude, purchasingUnit, installationUnit, installDate, contacts, eMI,
      deviceMagnitude, configIntensity, earthquakeOpenOrnot, switch1PullIn,
      switch1ReleaseValue, switch2PullIn, switch2ReleaseValue, switch3PullIn,
      switch3ReleaseValue, switch4PullIn, centerName, switch4ReleaseValue,
      address, baudRate, pressureUnit, decimalPoint, rangeL, rangeH,
      filterValue, h_H_LimitAlarm, h_LimitAlarm, l_L_LimitAlarm, l_LimitAlarm,
      solenoidValveOnOff, solenoidValveOnState, ntpOrNot, factoryId,
      installationAddress, configontacts, lAlarmOrNot, unitId, equipmentCall,
      manAutoSwitch, delayTime
-- 波特率转换(波特率转波特率代号)
local function baudRate2Mark(baudRateReceive)
    local baudRateMarkTransfer
    if baudRateReceive == 1200 then
        baudRateMarkTransfer = "0X0000"
    elseif baudRateReceive == 2400 then
        baudRateMarkTransfer = "0X0001"
    elseif baudRateReceive == 4800 then
        baudRateMarkTransfer = "0X0002"
    elseif baudRateReceive == 9600 then
        baudRateMarkTransfer = "0X0003"
    elseif baudRateReceive == 19200 then
        baudRateMarkTransfer = "0X0004"
    elseif baudRateReceive == 38400 then
        baudRateMarkTransfer = "0X0005"
    elseif baudRateReceive == 57600 then
        baudRateMarkTransfer = "0X0006"
    elseif baudRateReceive == 115200 then
        baudRateMarkTransfer = "0X0007"
    end
    return baudRateMarkTransfer
end
-- 压力单位转换(压力单位转单位代号)
local function unit2Mark(pressureUnitReceive)
    local unitMarkTransfer
    if pressureUnitReceive == "Mpa" then
        unitMarkTransfer = "0X0000"
    elseif pressureUnitReceive == "Kpa" then
        unitMarkTransfer = "0X0001"
    elseif pressureUnitReceive == "Pa" then
        unitMarkTransfer = "0X0002"
    elseif pressureUnitReceive == "Bar" then
        unitMarkTransfer = "0X0003"
    elseif pressureUnitReceive == "mbar" then
        unitMarkTransfer = "0X0004"
    elseif pressureUnitReceive == "kg/cm2" then
        unitMarkTransfer = "0X0005"
    elseif pressureUnitReceive == "psi" then
        unitMarkTransfer = "0X0006"
    elseif pressureUnitReceive == "mh^2o" then
        unitMarkTransfer = "0X0007"
    elseif pressureUnitReceive == "mmh^2o" then
        unitMarkTransfer = "0X0008"
    end
    return unitMarkTransfer
end
-- 获取keson服务器json数据
local function getKesonJsonData(jsonData)
    if jsonData["devtype"] then devtypeGet = jsonData["devtype"] end
    if jsonData["seq"] then seqGet = jsonData["seq"] end
    -- 系统参数处理
    if jsonData["parameter"] then
        if jsonData["parameter"]["ct"] then -- 设备时间 采用最小范围生效原则，毫秒
            ct = tonumber(jsonData["parameter"]["ct"])
        end
        if jsonData["parameter"]["sysId"] then
            sysId = jsonData["parameter"]["sysId"] -- 系统标识
        end
        if jsonData["parameter"]["pKey"] then
            pKey = jsonData["parameter"]["pKey"] -- 厂家编码
        end
        if jsonData["parameter"]["deviceTypeId"] then
            deviceTypeId = jsonData["parameter"]["deviceTypeId"] -- 设备类型Id
        end
        if jsonData["parameter"]["ntpOrNot"] then
            ntpOrNot = tostring(jsonData["parameter"]["ntpOrNot"]) -- 是否手动网络授时
        end
        if jsonData["parameter"]["dc"] then
            dc = tonumber(jsonData["parameter"]["dc"]) -- 上报周期（s)
        end
        if jsonData["parameter"]["factoryId"] then
            factoryId = jsonData["parameter"]["factoryId"] --
        end
        if jsonData["parameter"]["purchasingUnit"] then
            purchasingUnit = jsonData["parameter"]["purchasingUnit"]
        end
        if jsonData["parameter"]["installationUnit"] then
            installationUnit = jsonData["parameter"]["installationUnit"]
        end
        if jsonData["parameter"]["installDate"] then
            installDate = jsonData["parameter"]["installDate"]
        end
        if jsonData["parameter"]["contacts"] then
            contacts = jsonData["parameter"]["contacts"]
        end
        if jsonData["parameter"]["eMI"] then
            eMI = jsonData["parameter"]["eMI"]
        end
        if jsonData["parameter"]["deviceMagnitude"] then
            deviceMagnitude = tonumber(jsonData["parameter"]["deviceMagnitude"])
        end
        if jsonData["parameter"]["configIntensity"] then
            configIntensity = tonumber(jsonData["parameter"]["configIntensity"])
        end
        if jsonData["parameter"]["earthquakeOpenOrnot"] then
            if jsonData["parameter"]["earthquakeOpenOrnot"] == "false" then
                earthquakeOpenOrnot = false
            elseif jsonData["parameter"]["earthquakeOpenOrnot"] == "true" then
                earthquakeOpenOrnot = true
            end
        end
        if jsonData["parameter"]["longitude"] then
            longitude = tonumber(jsonData["parameter"]["longitude"])
        end
        if jsonData["parameter"]["latitude"] then
            latitude = tonumber(jsonData["parameter"]["latitude"])
        end
        if jsonData["parameter"]["centerName"] then
            centerName = jsonData["parameter"]["centerName"]
        end
        if jsonData["parameter"]["installationAddress"] then
            installationAddress = jsonData["parameter"]["installationAddress"]
        end
        if jsonData["parameter"]["lAlarmOrNot"] then
            if jsonData["parameter"]["lAlarmOrNot"] == "false" then
                lAlarmOrNot = false
                log.info("lAlarmOrNotGetParam:", lAlarmOrNot)
            elseif jsonData["parameter"]["lAlarmOrNot"] == "true" then
                lAlarmOrNot = true
            end
        end
        if jsonData["parameter"]["unitId"] then
            unitId = tonumber(jsonData["parameter"]["unitId"])
        end
        if jsonData["parameter"]["mode"] then
            mode = jsonData["parameter"]["mode"]
        end
        if jsonData["parameter"]["manAutoSwitch"] then
            manAutoSwitch = tostring(jsonData["parameter"]["manAutoSwitch"])
        end
        if jsonData["parameter"]["delayTime"] then
            delayTime = tonumber(jsonData["parameter"]["delayTime"])
        end
    end
    -- 设备参数设置
    if jsonData["payload"] then
        if jsonData["payload"]["address"] then
            address = jsonData["payload"]["address"]
        end
        if jsonData["payload"]["solenoidValveOnOff"] then
            solenoidValveOnOff = tostring(
                                     jsonData["payload"]["solenoidValveOnOff"])
            log.info("solenoidValveOnOff:", solenoidValveOnOff)
        end
        if jsonData["payload"]["baudRate"] then
            baudRate = baudRate2Mark(tonumber(jsonData["payload"]["baudRate"]))
        end
        if jsonData["payload"]["pressureUnit"] then
            pressureUnit = unit2Mark(jsonData["payload"]["pressureUnit"])
        end
        if jsonData["payload"]["decimalPoint"] then
            decimalPoint = tonumber(jsonData["payload"]["decimalPoint"])
        end
        log.info("H_H_LimitAlarm:", jsonData["payload"]["hHLimitAlarm"])
        if jsonData["payload"]["hHLimitAlarm"] then
            h_H_LimitAlarm = tonumber(jsonData["payload"]["hHLimitAlarm"])
        end
        if jsonData["payload"]["hLimitAlarm"] then
            h_LimitAlarm = tonumber(jsonData["payload"]["hLimitAlarm"])
        end
        if jsonData["payload"]["lLLimitAlarm"] then
            l_L_LimitAlarm = tonumber(jsonData["payload"]["lLLimitAlarm"])
        end
        if jsonData["payload"]["lLimitAlarm"] then
            l_LimitAlarm = tonumber(jsonData["payload"]["lLimitAlarm"])
        end
        if jsonData["payload"]["rangeL"] then
            rangeL = tonumber(jsonData["payload"]["rangeL"])
        end
        if jsonData["payload"]["rangeH"] then
            rangeH = tonumber(jsonData["payload"]["rangeH"])
        end
        if jsonData["payload"]["filterValue"] then
            filterValue = jsonData["payload"]["filterValue"]
        end
        if jsonData["payload"]["switch1PullIn "] then
            switch1PullIn = jsonData["payload"]["switch1PullIn "]
        end
        if jsonData["payload"]["switch1ReleaseValue"] then
            switch1ReleaseValue = jsonData["payload"]["switch1ReleaseValue"]
        end
        if jsonData["payload"]["switch2PullIn"] then
            switch2PullIn = jsonData["payload"]["switch2PullIn"]
        end
        if jsonData["payload"]["switch2ReleaseValue"] then
            switch2ReleaseValue = jsonData["payload"]["switch2ReleaseValue"]
        end
        if jsonData["payload"]["switch3PullIn"] then
            switch3PullIn = jsonData["payload"]["switch3PullIn"]
        end
        if jsonData["payload"]["switch3ReleaseValue"] then
            switch3ReleaseValue = jsonData["payload"]["switch3ReleaseValue"]
        end
        if jsonData["payload"]["switch4PullIn"] then
            switch4PullIn = jsonData["payload"]["switch4PullIn"]
        end
        if jsonData["payload"]["switch4ReleaseValue"] then
            switch4ReleaseValue = jsonData["payload"]["switch4ReleaseValue"]
        end
    end
end
-- 新奥服务器json数据处理
local function getXinAoJsonData(jsonData)
    if jsonData["devtype"] then devtypeGet = jsonData["devtype"] end
    log.info("seq:", jsonData["seq"])
    if jsonData["seq"] then seqGet = jsonData["seq"] end
    -- 系统参数设置
    if jsonData["parameter"] then
        if jsonData["parameter"]["Ct"] then
            ct = tonumber(jsonData["parameter"]["Ct"])
        end
        if jsonData["parameter"]["SysId"] then
            sysId = jsonData["parameter"]["SysId"]
        end
        if jsonData["parameter"]["PKey"] then
            pKey = jsonData["parameter"]["PKey"]
        end
        if jsonData["parameter"]["DeviceTypeId"] then
            deviceTypeId = jsonData["parameter"]["DeviceTypeId"]
        end
        if jsonData["parameter"]["PurchasingUnit"] then
            purchasingUnit = jsonData["parameter"]["PurchasingUnit"]
        end
        if jsonData["parameter"]["InstallationUnit"] then
            installationUnit = jsonData["parameter"]["InstallationUnit"]
        end
        if jsonData["parameter"]["InstallDate"] then
            installDate = jsonData["parameter"]["InstallDate"]
        end
        if jsonData["parameter"]["Contacts"] then
            contacts = jsonData["parameter"]["Contacts"]
        end
        if jsonData["parameter"]["EMI"] then
            eMI = jsonData["parameter"]["EMI"]
        end
        if jsonData["parameter"]["LAlarmOrNot"] then
            if jsonData["parameter"]["LAlarmOrNot"] == "false" then
                lAlarmOrNot = false
            elseif jsonData["parameter"]["LAlarmOrNot"] == "true" then
                lAlarmOrNot = true
            end
        end
        if jsonData["parameter"]["DeviceMagnitude"] then
            deviceMagnitude = jsonData["parameter"]["DeviceMagnitude"]
        end
        if jsonData["parameter"]["ConfigIntensity"] then
            configIntensity = jsonData["parameter"]["ConfigIntensity"]
        end
        if jsonData["parameter"]["EarthquakeOpenOrnot"] then
            earthquakeOpenOrnot = jsonData["parameter"]["EarthquakeOpenOrnot"]
        end
        if jsonData["parameter"]["CenterName"] then
            centerName = jsonData["parameter"]["CenterName"]
        end
        if jsonData["parameter"]["ManAutoSwitch"] then
            manAutoSwitch = tostring(jsonData["parameter"]["ManAutoSwitch"])
        end
        if jsonData["parameter"]["DelayTime"] then
            delayTime = tonumber(jsonData["parameter"]["DelayTime"])
        end
    end
    -- 设备参数设置
    if jsonData["payload"]["Address"] and address then
        address = jsonData["payload"]["Address"]
    end
    if jsonData["payload"]["solenoidValveOnOff"] then
        solenoidValveOnOff = tostring(jsonData["payload"]["solenoidValveOnOff"])
    end
    if jsonData["payload"]["Dc"] then
        dc = tonumber(jsonData["payload"]["Dc"])
    end
    log.info("baudRateJson", jsonData["payload"]["BaudRate"])
    if jsonData["payload"]["BaudRate"] then
        baudRate = baudRate2Mark(tonumber(jsonData["payload"]["BaudRate"]))
        log.info("baudRate", baudRate)
    end
    if jsonData["payload"]["Mode"] then mode = jsonData["payload"]["Mode"] end
    if jsonData["payload"]["Longitude"] then
        longitude = tonumber(jsonData["payload"]["Longitude"])
    end
    if jsonData["payload"]["Latitude"] then
        latitude = tonumber(jsonData["payload"]["Latitude"])
    end
    if jsonData["payload"]["PressureUnit"] then
        pressureUnit = unit2Mark(jsonData["payload"]["PressureUnit"])
    end
    if jsonData["payload"]["DecimalPoint"] then
        decimalPoint = tonumber(jsonData["payload"]["DecimalPoint"])
    end
    log.info("H_H_LimitAlarm:", jsonData["payload"]["HHLimitAlarm"])
    if jsonData["payload"]["HHLimitAlarm"] then
        h_H_LimitAlarm = tonumber(jsonData["payload"]["HHLimitAlarm"])
    end
    if jsonData["payload"]["HLimitAlarm"] then
        h_LimitAlarm = tonumber(jsonData["payload"]["HLimitAlarm"])
    end
    if jsonData["payload"]["LLLimitAlarm"] then
        l_L_LimitAlarm = tonumber(jsonData["payload"]["LLLimitAlarm"])
    end
    if jsonData["payload"]["LLimitAlarm"] then
        l_LimitAlarm = tonumber(jsonData["payload"]["LLimitAlarm"])
    end
    if jsonData["payload"]["RangeL"] then
        rangeL = tonumber(jsonData["payload"]["RangeL"])
    end
    if jsonData["payload"]["RangeH"] then
        rangeH = tonumber(jsonData["payload"]["RangeH"])
    end
    if jsonData["payload"]["FilterValue"] then
        filterValue = jsonData["payload"]["FilterValue"]
    end
end
-- 系统参数设置
local function sysParamSet()
    SetParm = true
    if centerName ~= nvm.get("CenterName") and centerName then -- 地震中心名称存储
        nvm.set("CenterName", centerName)
    end
    if earthquakeOpenOrnot ~= nvm.get("EarthquakeOpenOrnot") and -- 地震预警开关状态存储
        earthquakeOpenOrnot then
        nvm.set("EarthquakeOpenOrnot", earthquakeOpenOrnot)
    end
    if configIntensity ~= nvm.get("ConfigIntensity") and configIntensity then -- 中心烈度设置
        nvm.set("ConfigIntensity", configIntensity)
    end
    if deviceMagnitude ~= nvm.get("DeviceMagnitude") and deviceMagnitude then -- 设备震级设置存储
        nvm.set("DeviceMagnitude", deviceMagnitude)
    end
    if eMI ~= nvm.get("EMI") and eMI then nvm.set("EMI", eMI) end -- EMI存储
    if contacts ~= nvm.get("Contacts") and contacts then
        nvm.set("Contacts", contacts)
    end
    if installDate ~= nvm.get("InstallDate") and installDate then -- 安装日期存储
        nvm.set("InstallDate", installDate)
    end
    if installationUnit ~= nvm.get("InstallationUnit") and installationUnit then -- 存储单位
        nvm.set("InstallationUnit", installationUnit)
    end
    if purchasingUnit ~= nvm.get("PurchasingUnit") and purchasingUnit then -- 采购单位
        nvm.set("PurchasingUnit", purchasingUnit)
    end
    if devtypeGet ~= nvm.get("Devtype") and devtypeGet then -- 设备类型
        nvm.set("Devtype", devtypeGet)
    end
    if lAlarmOrNot ~= nvm.get("LAlarmOrNot") and lAlarmOrNot ~= nil then -- 低压报警开启标志0-不开启 1-开启
        log.info("LAlarmOrNot设置！！！", lAlarmOrNot)
        nvm.set("LAlarmOrNot", lAlarmOrNot)
    end
    log.info("LAlarmOrNot", lAlarmOrNot)
    if factoryId ~= nvm.get("FactoryId") and factoryId then -- 厂内设备身份id，无线报警关联身份识别
        log.info("factoryId设置！！！")
        nvm.set("FactoryId", factoryId)
    end
    if delayTime ~= nvm.get("DelayTime") and delayTime then -- 厂内设备身份id，无线报警关联身份识别
        nvm.set("DelayTime", delayTime)
    end
    if manAutoSwitch ~= nvm.get("ManAutoSwitch") and manAutoSwitch then -- 厂内设备身份id，无线报警关联身份识别
        nvm.set("ManAutoSwitch", manAutoSwitch)
    end
    log.info("dcRestore2", dc)
    if dc ~= nvm.get("Dc") and dc then -- 上报周期
        log.info("dcRestore2", dc)
        if dc > 0 and dc < 60 then dc = 60 end
        nvm.set("Dc", dc)
        if dc == 0 then
            log.info("上报周期为0")
            regularlyUploadData.stopTimeDataUploadTask()
        elseif dc > 0 then
            log.info("上报周期为:", dc)
            regularlyUploadData.realTimeDataUpload(dc)
        end
    end
    if ntpOrNot ~= nvm.get("NtpOrNot") and ntpOrNot then -- 手动授时
        if ntpOrNot == "1" then networkTiming.setNetworkTimingByManual() end
        nvm.set("ntpOrNot", ntpOrNot)
    end
    if ct ~= nvm.get("Ct") and ct then nvm.set("Ct", ct) end -- 设备时间
    if pKey ~= nvm.get("PKey") and pkey then nvm.set("PKey", pKey) end
    if sysId ~= nvm.get("SysId") and sysId then nvm.set("SysId", sysId) end
    if deviceTypeId ~= nvm.get("DeviceTypeId") and deviceTypeId then -- 设备类型
        nvm.set("DeviceTypeId", deviceTypeId)
    end
    if mode ~= nvm.get("Mode") and mode then nvm.set("Mode", mode) end -- 上报方式
    if longitude ~= nvm.get("Longitude") and longitude then -- 经度
        nvm.set("Longitude", longitude)
    end
    if latitude ~= nvm.get("Latitude") and latitude then -- 纬度
        nvm.set("Latitude", latitude)
    end
end
-- 设备参数设置
local function deviceParamSet(modbusReturnResult)
    if address ~= nvm.get("Address") and address then -- 从机地址
        reg = "0x0000"
        modbusReturnResult = modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                                   address)
        sys.wait(100)
    end
    if baudRate ~= nvm.get("BaudRate") and baudRate then -- 波特率
        if modbusReturnResult then
            log.info("baudRate 设置")
            reg = "0x0001"
            modbusrtu.modbus_send(slaveaddr, instructions, reg, baudRate)
            sys.wait(100)
        end
    end
    if pressureUnit ~= nvm.get("PressureUnit") and pressureUnit then -- 压力单位
        if modbusReturnResult then
            reg = "0x0002"
            modbusReturnResult = modbusrtu.Modbus_send(slaveaddr, instructions,
                                                       reg, pressureUnit)
            sys.wait(100)
        end
    end
    -- 电磁阀通断控制
    log.info("solenoidValveOnOff:", solenoidValveOnOff) -- 电磁阀远程控制
    if solenoidValveOnOff ~= nvm.get("solenoidValveOnOff") and
        solenoidValveOnOff then -- 小数点设置
        nvm.set("solenoidValveOnOff", solenoidValveOnOff)
        setSoLen = true
    end
    if solenoidValveOnOff == "0" then -- 电磁阀吸合，断气
        setGpio16Fnc(0)
        nvm.set("solenoidValveOnState", "0")
    elseif solenoidValveOnOff == "1" then -- 电磁阀释放，通气
        setGpio16Fnc(1)
    end
    if decimalPoint ~= nvm.get("DecimalPoint") and decimalPoint then -- 小数点设置
        if modbusReturnResult then
            reg = "0x0003"
            modbusReturnResult = modbusrtu.Modbus_send(slaveaddr, instructions,
                                                       reg, decimalPoint)
            sys.wait(100)
        end
    end
    decimalPoint = nvm.get("DecimalPoint")
    if rangeL ~= nvm.get("RangeL") and rangeL then -- 量程低值设置
        if modbusReturnResult then
            reg = "0x0005"
            modbusReturnResult = modbusrtu.Modbus_send(slaveaddr, instructions,
                                                       reg, rangeL * 10 ^
                                                           decimalPoint)
            sys.wait(100)
        end
    end
    if rangeH ~= nvm.get("RangeH") and rangeH then -- 量程高值设置
        if modbusReturnResult then
            reg = "0x0006"
            log.info("RangeH", rangeH)
            modbusReturnResult = modbusrtu.Modbus_send(slaveaddr, instructions,
                                                       reg, rangeH * 10 ^
                                                           decimalPoint)
            sys.wait(100)
        end
    end
    if filterValue ~= nvm.get("FilterValue") and filterValue then -- 滤波值设置
        if modbusReturnResult then
            reg = "0x0007"
            modbusReturnResult = modbusrtu.Modbus_send(slaveaddr, instructions,
                                                       reg, filterValue)
            sys.wait(100)
        end
    end
    if l_L_LimitAlarm ~= nvm.get("L_L_LimitAlarm") and l_L_LimitAlarm then -- 低低限设置
        nvm.set("L_L_LimitAlarm", l_L_LimitAlarm)
        if modbusReturnResult then
            log.info("lAlarmOrNot", lAlarmOrNot)
            if lAlarmOrNot == true then -- 低限报警表标志，true-低限报警打开，false-低限报警关闭
                log.info("低低吸合值低切断")
                if l_L_LimitAlarm >= (nvm.get("L_L_LimitAlarm") + 0.1) then -- 防止设置低低限时电磁阀误跳  原理：防止低限变高限
                    reg = "0x0009"
                    nvm.set("Switch1ReleaseValue",
                            (l_L_LimitAlarm + 0.1) * 10 ^ decimalPoint)
                    modbusReturnResult =
                        modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                              tostring(
                                                  (l_L_LimitAlarm + 0.1) * 10 ^
                                                      decimalPoint))
                    sys.wait(100)
                    if modbusReturnResult then
                        nvm.set("Switch1PullIn",
                                l_L_LimitAlarm * 10 ^ decimalPoint)
                        reg = "0x0008"
                        modbusReturnResult =
                            modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                                  tostring(
                                                      l_L_LimitAlarm * 10 ^
                                                          decimalPoint))
                        sys.wait(100)
                    end
                elseif l_L_LimitAlarm < (nvm.get("L_L_LimitAlarm") + 0.1) then
                    reg = "0x0008"
                    nvm.set("Switch1PullIn", l_L_LimitAlarm * 10 ^ decimalPoint)
                    modbusReturnResult =
                        modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                              tostring(
                                                  l_L_LimitAlarm * 10 ^
                                                      decimalPoint))
                    sys.wait(100)
                    if modbusReturnResult then
                        nvm.set("Switch1ReleaseValue",
                                (l_L_LimitAlarm + 0.1) * 10 ^ decimalPoint)
                        reg = "0x0009"
                        modbusReturnResult =
                            modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                                  tostring(
                                                      (l_L_LimitAlarm + 0.1) *
                                                          10 ^ decimalPoint))
                        sys.wait(100)
                    end
                end
            elseif lAlarmOrNot == false then
                log.info("低低吸合值低不切断")
                nvm.set("Switch1PullIn", 0)
                nvm.set("Switch1ReleaseValue", 0)
                reg = "0x0008"
                modbusReturnResult = modbusrtu.Modbus_send(slaveaddr,
                                                           instructions, reg,
                                                           "0")
                sys.wait(100)
                if modbusReturnResult then
                    reg = "0x0009"
                    modbusReturnResult =
                        modbusrtu.Modbus_send(slaveaddr, instructions, reg, "0")
                    sys.wait(100)
                end
            end
        end
    end
    log.info("l_LimitAlarm", l_LimitAlarm, "nvmL_LimitAlarm:",
             tostring(nvm.get("L_LimitAlarm")))
    if l_LimitAlarm ~= nvm.get("L_LimitAlarm") and l_LimitAlarm then -- 低限设置
        nvm.set("L_LimitAlarm", l_LimitAlarm)
        log.info("低限设置1", modbusReturnResult)
        if modbusReturnResult then
            log.info("低限设置2")
            if lAlarmOrNot == true then -- 同低低限
                if l_LimitAlarm >= (nvm.get("L_LimitAlarm") + 0.1) then
                    reg = "0x000B"
                    nvm.set("Switch2ReleaseValue",
                            (l_LimitAlarm + 0.1) * 10 ^ decimalPoint)
                    modbusReturnResult =
                        modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                              tostring(
                                                  (l_LimitAlarm + 0.1) * 10 ^
                                                      decimalPoint))
                    sys.wait(100)
                    if modbusReturnResult then
                        reg = "0x000A"
                        nvm.set("Switch2PullIn",
                                l_LimitAlarm * 10 ^ decimalPoint)
                        modbusReturnResult =
                            modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                                  tostring(
                                                      l_LimitAlarm * 10 ^
                                                          decimalPoint))
                        sys.wait(100)
                    end
                elseif l_LimitAlarm < (nvm.get("L_LimitAlarm") + 0.1) then
                    reg = "0x000A"
                    nvm.set("Switch2PullIn", l_LimitAlarm * 10 ^ decimalPoint)
                    modbusReturnResult =
                        modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                              tostring(
                                                  l_LimitAlarm * 10 ^
                                                      decimalPoint))
                    sys.wait(100)
                    if modbusReturnResult then
                        reg = "0x000B"
                        nvm.set("Switch2ReleaseValue",
                                (l_LimitAlarm + 0.1) * 10 ^ decimalPoint)
                        modbusReturnResult =
                            modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                                  tostring(
                                                      (l_LimitAlarm + 0.1) * 10 ^
                                                          decimalPoint))
                        sys.wait(100)
                    end
                end
            elseif lAlarmOrNot == false then
                log.info("低吸合值低不报")
                nvm.set("Switch2PullIn", 0)
                nvm.set("Switch2ReleaseValue", 0)
                reg = "0x000A"
                modbusReturnResult = modbusrtu.Modbus_send(slaveaddr,
                                                           instructions, reg,
                                                           "0")
                sys.wait(100)
                if modbusReturnResult then
                    reg = "0x000B"
                    modbusReturnResult =
                        modbusrtu.Modbus_send(slaveaddr, instructions, reg, "0")
                    sys.wait(100)
                end
            end
        end
    end
    log.info("h_LimitAlarm", h_LimitAlarm)
    if h_LimitAlarm ~= nvm.get("H_LimitAlarm") and h_LimitAlarm then -- 高限设置
        nvm.set("H_LimitAlarm", h_LimitAlarm)
        if modbusReturnResult then
            if h_LimitAlarm <= (nvm.get("H_LimitAlarm") - 0.1) then -- 防止设置误跳
                log.info("高报小于设定值")
                reg = "0x000D"
                nvm.set("Switch3ReleaseValue",
                        (h_LimitAlarm - 0.1) * 10 ^ decimalPoint)
                modbusReturnResult = modbusrtu.Modbus_send(slaveaddr,
                                                           instructions, reg,
                                                           tostring(
                                                               (h_LimitAlarm -
                                                                   0.1) * 10 ^
                                                                   decimalPoint))
                sys.wait(100)
                if modbusReturnResult then
                    reg = "0x000C"
                    nvm.set("Switch3PullIn", h_LimitAlarm * 10 ^ decimalPoint)
                    modbusReturnResult =
                        modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                              tostring(
                                                  h_LimitAlarm * 10 ^
                                                      decimalPoint))
                    sys.wait(100)
                end
            elseif h_LimitAlarm > (nvm.get("H_LimitAlarm") - 0.1) then
                log.info("高报大于设定值")
                reg = "0x000C"
                nvm.set("Switch3PullIn", h_LimitAlarm * 10 ^ decimalPoint)
                modbusReturnResult = modbusrtu.Modbus_send(slaveaddr,
                                                           instructions, reg,
                                                           tostring(
                                                               h_LimitAlarm * 10 ^
                                                                   decimalPoint))
                sys.wait(100)
                if modbusReturnResult then
                    reg = "0x000D"
                    nvm.set("Switch3ReleaseValue",
                            (h_LimitAlarm - 0.1) * 10 ^ decimalPoint)
                    modbusReturnResult =
                        modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                              tostring(
                                                  (h_LimitAlarm - 0.1) * 10 ^
                                                      decimalPoint))
                    sys.wait(100)
                end
            end
        end
    end
    log.info("h_H_LimitAlarm", h_H_LimitAlarm)
    if h_H_LimitAlarm ~= nvm.get("H_H_LimitAlarm") and h_H_LimitAlarm then -- 高高限设置
        nvm.set("H_H_LimitAlarm", h_H_LimitAlarm)
        if modbusReturnResult then
            if h_H_LimitAlarm <= (nvm.get("H_H_LimitAlarm") - 0.1) then
                log.info("高切小于设定值")
                reg = "0x000F"
                nvm.set("Switch4ReleaseValue",
                        (h_H_LimitAlarm - 0.1) * 10 ^ decimalPoint)
                modbusReturnResult = modbusrtu.Modbus_send(slaveaddr,
                                                           instructions, reg,
                                                           tostring(
                                                               (h_H_LimitAlarm -
                                                                   0.1) * 10 ^
                                                                   decimalPoint))
                sys.wait(100)
                if modbusReturnResult then
                    reg = "0x000E"
                    nvm.set("Switch4PullIn", h_H_LimitAlarm * 10 ^ decimalPoint)
                    modbusReturnResult =
                        modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                              tostring(
                                                  h_H_LimitAlarm * 10 ^
                                                      decimalPoint))
                    sys.wait(100)
                end
            elseif h_H_LimitAlarm > (nvm.get("H_H_LimitAlarm") - 0.1) then
                log.info("高切大于设定值")
                reg = "0x000E"
                nvm.set("Switch4PullIn", h_H_LimitAlarm * 10 ^ decimalPoint)
                modbusReturnResult = modbusrtu.Modbus_send(slaveaddr,
                                                           instructions, reg,
                                                           tostring(
                                                               h_H_LimitAlarm *
                                                                   10 ^
                                                                   decimalPoint))
                sys.wait(100)
                if modbusReturnResult then
                    reg = "0x000F"
                    nvm.set("Switch4ReleaseValue",
                            (h_H_LimitAlarm - 0.1) * 10 ^ decimalPoint)
                    modbusReturnResult =
                        modbusrtu.Modbus_send(slaveaddr, instructions, reg,
                                              tostring(
                                                  (h_H_LimitAlarm - 0.1) * 10 ^
                                                      decimalPoint))
                    sys.wait(100)
                end
            end
        end
    end
    if reg ~= "" and reg ~= "0xfffe" then
        if modbusReturnResult then
            reg = "0xFFFE"
            modbusReturnResult = modbusrtu.Modbus_send(slaveaddr, instructions,
                                                       reg, "0")
        end
    end
    if modbusReturnResult and not setSoLen then
        sys.restart("设置参数完成，重启设备！")
    end

    setSoLen = false

    mqttInMsgKeson.ParamSetOrNot = false
end
-- 参数配置修改
function ParamSet(jsonData, modbusReturnResult, serverId)
    -- 服务器端参数处理
    if serverId == "keson" then
        getKesonJsonData(jsonData)
    elseif serverId == "XinAo" then
        getXinAoJsonData(jsonData)
    end
    -- 系统参数设置
    sys.taskInit(sysParamSet)
    -- 设备参数设置
    -- modbus_send("0x01","0x01","0x0101","0x04")
    sys.taskInit(deviceParamSet, modbusReturnResult)
end
